"use strict";
/* Controllers */
app.controller("resolvegroupListController", ['$scope', '$location', '$rootScope', '$routeParams', 'resolvegroupListService', function ($scope, $location, $rootScope, $routeParams, resolvegroupListService) {

    var vm = $scope.vm = {};
    vm.p = {};
    vm.result = {};
    //vm.p.appID = $routeParams.appID; //  获取应用模块的名称

    //datetimePicker('datetimepicker','myDate');
    //if($rootScope.ds==null) {
    //    vm.ds = getDateStr(-1);
    //    $rootScope.ds = getDateStr(-1);
    //}else {
    //    vm.ds = $rootScope.ds;
    //}

    //vm.dim = 'week';
    //vm.dims = {};
    //vm.dims.items = [{idx:0,label:'日账单',id:'day'},{idx:1,label:'周账单',id:'week'}];
    //if($rootScope.dim==null) {
    //    vm.dim = vm.dims.items[0];
    //    $rootScope.dim = vm.dim;
    //}else {
    //    vm.dim = vm.dims.items[$rootScope.dim.idx];
    //}
    //$scope.vm.onDimSelected = function(id) {
    //    $scope.initializeController();
    //    $rootScope.dim = $scope.vm.dim;
    //};

    var errorFn = function (result) {
        alert(result);
    };

    $scope.listResolvegroup = function () {
        var vm = $scope.vm;
        vm.result.ms = "...";
        var params = {};
        var procFn = function (response) {
            vm.result.ms = response.ms;
            var data = response.data;
            if (data == null) return;
            vm.result.data = data;
            $('#list_table').bootstrapTable('load', data);
        };
        resolvegroupListService.listResolvegroup(params, procFn, errorFn);
    };

    $scope.initializeController = function () {
        vm.result.ms = '';
        var $resultTable = $('#list_table');
        $resultTable.bootstrapTable({
            pageList: [30, 50, 100, 300, 500, 1000], pageSize: 30
        });


        $resultTable.bootstrapTable('hideColumn', 'ondutyMode');
        $resultTable.bootstrapTable('hideColumn', 'rotationTime');
        $resultTable.bootstrapTable('hideColumn', 'isOnduty');
        $scope.listResolvegroup();
    };

    $scope.initializeController();

}]);


/* Services */
app.service('resolvegroupListService', ['ajaxService', function (ajaxService) {

    this.listResolvegroup = function (params, successFunction, errorFunction) {
        ajaxService.AjaxPost(params, "/star/api/onduty-app-service/v1/resolvegroup/listResolvegroup.do", successFunction, errorFunction);
    };
}]);

function resolvegroupIDOptFmt(value, row, index) {
    if (value == null) {
        return "-";
    }
    return [
        '<a href="#/star/onduty/resolvegroup_add/id/', value, '" target="_blank">', "<u>修改</u>", '</a>',
        '<a href="#/star/onduty/scheduler_list/', row.resolvegroupId, '" target="_blank">', " <u>值班</u>", '</a>'
    ].join('');
}

function citFormat(value, row, index) {
    var addIt = '<a style="float:right;margin-top:10px;color:#eee;" href="#/star/onduty/cti_add/rg/'+row.resolvegroupId+'" target="_blank"><u>新增</u></a>';
    if (value == null) {
        return "-" +'<br/>'+ addIt;
    }
    var ctis = value.split(",");
    var arr = [];
    for (var i in ctis) {
        var item = ctis[i];
        var cti_cnt=item.split("|")
        arr.push('<a href="#/star/onduty/cti_add/id/' + cti_cnt[1] + '" target="_blank">' + cti_cnt[0] + '</a>'+'<br/>（'+cti_cnt[1]+'）')
    }
    arr.push(addIt)
    return arr.join('<br/>');
}

//function agentFormat(value, row, index) {
//    var addIt = '<a style="float:right;margin-top:10px;color:#eee;" href="#/star/onduty/agent_add/rg/'+row.resolvegroupId+'" target="_blank"><u>新增</u></a>';
//    if (value == null) {
//        return "-"+'<br/>'+addIt;
//    }
//    var arr = value.split(",");
//    var ret = [];
//    for (var i in arr) {
//        var item = arr[i];
//        var it=item.split("|")
//        ret.push('<a href="#/star/onduty/agent_add/id/' + it[1] + '" target="_blank">' + it[0] + '</a>'+'<br/>（'+it[2]+'）')
//    }
//    ret.push(addIt)
//    return ret.join('<br/>');
//}

function ondutyModeFmt(value, row, index) {
    if (value == 'everyday') {
        return "每天值班"
    } else if (value == 'workday') {
        return "工作日值班"
    } else {
        return "-"
    }
}

function usersFormat(value, row, index) {
    if (value == null) {
        return "-";
    }
    var users = value.split(",");
    var arr = [];
    for (var i in users) {
        var item = users[i];
        arr.push(item)
    }
    return arr.join('<br/>');
}

function mastersFormat(value, row, index) {
    if (value == null) {
        return "-";
    }
    var master = '';
    if(row.masters0!=null) {
        master = row.masters0.split(",")[0];
    }
    var users = value.split(",");


    var parseId = function(str) {
        var index1=str.indexOf('(')
        var index2=str.indexOf(')')
        return str.substring(index1+1,index2)
    }
    var arr = [];
    var isOwner = false;
    for (var i in users) {
        var item = users[i];
        if (! isOwner) {
            var oa = parseId(item);
            if(master=='' || oa==master) {
                isOwner = true;
                // 增加负责人标识
                item = "*" + item;
            }
        }
        arr.push(item);
    }
    arr.sort();
    return arr.join('<br/>');
}
function datetimeFmt(value, row, index) {
    if (value == null) {
        return "-";
    }
    return value.substr(0, 16);
}

function rgFormat(value, row, index) {
    if (value == null) {
        return "-";
    }
    return row.name+"<br/> ("+row.resolvegroupId+")"
}

function isOndutyFormat(value, row, index) {
    if (value == 0) {
        return "否"
    } else if (value == 1) {
        return "是"
    } else {
        return "-"
    }
}